/*
 * RecognizerView.java
 */
package recognizer;

import java.awt.Color;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import javax.swing.DefaultListModel;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import recognizer.neuron.Neuron;
import recognizer.neuron.NeuronNetwork;

/**
 * The application's main frame.
 */
public class RecognizerView extends FrameView {

    /**
     * Персептрон
     */
    NeuronNetwork perceptron;
    /**
     * Факты для обучения
     */
    HashMap<boolean[], String> facts = new HashMap<boolean[], String>();

    public RecognizerView(SingleFrameApplication app) {
        super(app);

        initComponents();
        jComboBoxAddToTraining.setVisible(false);
        jButtonAddToTrainingOk.setVisible(false);
        ArrayList<String> names = new ArrayList<String>();
        names.add("Крестик");

        facts.put(new boolean[]{true, false, false, false, true,
                    false, true, false, true, false,
                    false, false, true, false, false,
                    false, true, false, true, false,
                    true, false, false, false, true}, "Крестик");

//        facts.put( new boolean[] { true, false, true,
//                                   false, true, false,
//                                   false, false, false}, "Улыбка");
//
//        facts.put( new boolean[] { true, false, true,
//                                   false, true, false,
//                                   true, false, true}, "Крестик");
//
//        facts.put( new boolean[] { false, false, false,
//                                   false, true, false,
//                                   true, false, true}, "Грусть");

        perceptron = new NeuronNetwork(paintGrid1.SegmentsX * paintGrid1.SegmentsY, names);

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = RecognizerApp.getApplication().getMainFrame();
            aboutBox = new RecognizerAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        RecognizerApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        mainPanel = new javax.swing.JPanel();
        jPanelButtons = new javax.swing.JPanel();
        jButtonTraining = new javax.swing.JButton();
        jButtonRecognize = new javax.swing.JButton();
        jPanelAddToTraining = new javax.swing.JPanel();
        jButtonAddToTraining = new javax.swing.JButton();
        jComboBoxAddToTraining = new javax.swing.JComboBox();
        jButtonAddToTrainingOk = new javax.swing.JButton();
        paintGrid1 = new recognizer.PaintGrid();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListRecognized = new javax.swing.JList();
        jPanelTools = new javax.swing.JPanel();
        jButtonClear = new javax.swing.JButton();
        jSliderPaintSize = new javax.swing.JSlider();
        jCheckBoxEqualZoom = new javax.swing.JCheckBox();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                mainPanelMouseMoved(evt);
            }
        });

        jPanelButtons.setName("jPanelButtons"); // NOI18N
        jPanelButtons.setLayout(new java.awt.GridBagLayout());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(recognizer.RecognizerApp.class).getContext().getResourceMap(RecognizerView.class);
        jButtonTraining.setFont(resourceMap.getFont("jButtonRecognize.font")); // NOI18N
        jButtonTraining.setText(resourceMap.getString("jButtonTraining.text")); // NOI18N
        jButtonTraining.setMaximumSize(new java.awt.Dimension(200, 100));
        jButtonTraining.setMinimumSize(new java.awt.Dimension(100, 0));
        jButtonTraining.setName("jButtonTraining"); // NOI18N
        jButtonTraining.setPreferredSize(new java.awt.Dimension(150, 23));
        jButtonTraining.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonTrainingMouseClicked(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 20;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        jPanelButtons.add(jButtonTraining, gridBagConstraints);

        jButtonRecognize.setFont(resourceMap.getFont("jButtonRecognize.font")); // NOI18N
        jButtonRecognize.setText(resourceMap.getString("jButtonRecognize.text")); // NOI18N
        jButtonRecognize.setMaximumSize(new java.awt.Dimension(400, 100));
        jButtonRecognize.setMinimumSize(new java.awt.Dimension(0, 0));
        jButtonRecognize.setName("jButtonRecognize"); // NOI18N
        jButtonRecognize.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonRecognizeMouseClicked(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 50;
        gridBagConstraints.ipady = 50;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        jPanelButtons.add(jButtonRecognize, gridBagConstraints);

        jPanelAddToTraining.setAlignmentX(0.0F);
        jPanelAddToTraining.setAlignmentY(0.0F);
        jPanelAddToTraining.setMinimumSize(new java.awt.Dimension(376, 0));
        jPanelAddToTraining.setName("jPanelAddToTraining"); // NOI18N
        jPanelAddToTraining.setLayout(new java.awt.GridBagLayout());

        jButtonAddToTraining.setFont(resourceMap.getFont("jButtonRecognize.font")); // NOI18N
        jButtonAddToTraining.setText(resourceMap.getString("jButtonAddToTraining.text")); // NOI18N
        jButtonAddToTraining.setMaximumSize(new java.awt.Dimension(200, 100));
        jButtonAddToTraining.setMinimumSize(new java.awt.Dimension(0, 0));
        jButtonAddToTraining.setName("jButtonAddToTraining"); // NOI18N
        jButtonAddToTraining.setPreferredSize(new java.awt.Dimension(150, 23));
        jButtonAddToTraining.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonAddToTrainingMouseClicked(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.ipady = 50;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        jPanelAddToTraining.add(jButtonAddToTraining, gridBagConstraints);

        jComboBoxAddToTraining.setEditable(true);
        jComboBoxAddToTraining.setFont(resourceMap.getFont("jComboBoxAddToTraining.font")); // NOI18N
        jComboBoxAddToTraining.setName("jComboBoxAddToTraining"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.ipady = 50;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        jPanelAddToTraining.add(jComboBoxAddToTraining, gridBagConstraints);

        jButtonAddToTrainingOk.setFont(resourceMap.getFont("jButtonRecognize.font")); // NOI18N
        jButtonAddToTrainingOk.setText(resourceMap.getString("jButtonAddToTrainingOk.text")); // NOI18N
        jButtonAddToTrainingOk.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jButtonAddToTrainingOk.setName("jButtonAddToTrainingOk"); // NOI18N
        jButtonAddToTrainingOk.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonAddToTrainingOkMouseClicked(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 50;
        gridBagConstraints.ipady = 50;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        jPanelAddToTraining.add(jButtonAddToTrainingOk, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 20;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        jPanelButtons.add(jPanelAddToTraining, gridBagConstraints);

        paintGrid1.setBackground(resourceMap.getColor("paintGrid1.background")); // NOI18N
        paintGrid1.setName("paintGrid1"); // NOI18N
        paintGrid1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                paintGrid1MouseEntered(evt);
            }
        });

        javax.swing.GroupLayout paintGrid1Layout = new javax.swing.GroupLayout(paintGrid1);
        paintGrid1.setLayout(paintGrid1Layout);
        paintGrid1Layout.setHorizontalGroup(
            paintGrid1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        paintGrid1Layout.setVerticalGroup(
            paintGrid1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 177, Short.MAX_VALUE)
        );

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jListRecognized.setFont(resourceMap.getFont("jListRecognized.font")); // NOI18N
        jListRecognized.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jListRecognized.setName("jListRecognized"); // NOI18N
        jScrollPane1.setViewportView(jListRecognized);

        jPanelTools.setName("jPanelTools"); // NOI18N

        jButtonClear.setText(resourceMap.getString("jButtonClear.text")); // NOI18N
        jButtonClear.setName("jButtonClear"); // NOI18N
        jButtonClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonClearActionPerformed(evt);
            }
        });

        jSliderPaintSize.setMajorTickSpacing(10);
        jSliderPaintSize.setMaximum(40);
        jSliderPaintSize.setMinimum(5);
        jSliderPaintSize.setMinorTickSpacing(1);
        jSliderPaintSize.setPaintTicks(true);
        jSliderPaintSize.setToolTipText(resourceMap.getString("jSliderPaintSize.toolTipText")); // NOI18N
        jSliderPaintSize.setValue(20);
        jSliderPaintSize.setName("jSliderPaintSize"); // NOI18N
        jSliderPaintSize.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderPaintSizeStateChanged(evt);
            }
        });

        jCheckBoxEqualZoom.setSelected(false);
        jCheckBoxEqualZoom.setText(resourceMap.getString("jCheckBoxEqualZoom.text")); // NOI18N
        jCheckBoxEqualZoom.setName("jCheckBoxEqualZoom"); // NOI18N
        jCheckBoxEqualZoom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxEqualZoomActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelToolsLayout = new javax.swing.GroupLayout(jPanelTools);
        jPanelTools.setLayout(jPanelToolsLayout);
        jPanelToolsLayout.setHorizontalGroup(
            jPanelToolsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelToolsLayout.createSequentialGroup()
                .addComponent(jButtonClear)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSliderPaintSize, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxEqualZoom, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanelToolsLayout.setVerticalGroup(
            jPanelToolsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jButtonClear, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE)
            .addComponent(jCheckBoxEqualZoom, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE)
            .addComponent(jSliderPaintSize, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelButtons, javax.swing.GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanelTools, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(paintGrid1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jPanelTools, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(paintGrid1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelButtons, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(recognizer.RecognizerApp.class).getContext().getActionMap(RecognizerView.class, this);
        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setText(resourceMap.getString("aboutMenuItem.text")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 536, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 366, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Нажатие на кнопку "Обучить"
     * @param evt 
     */
private void jButtonTrainingMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonTrainingMouseClicked
    perceptron.training(facts);
}//GEN-LAST:event_jButtonTrainingMouseClicked
    /**
     * Нажатие на кнопку "Распознать"
     * @param evt 
     */
private void jButtonRecognizeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonRecognizeMouseClicked
    perceptron.recognize(paintGrid1.checkedSegments);
    DefaultListModel listModel = new DefaultListModel();
    Neuron neuron = null;
    for (Object object : perceptron.getNeurons()) {
        neuron = (Neuron) object;
        listModel.addElement((neuron.getState() ? "V " : "") + neuron.getName() + " "
                + (neuron.getThreshold() - neuron.getWeighedSum()));

        //System.out.println(neuron.getName() + " " + neuron.getWeighedSum());
    }
    jListRecognized.setModel(listModel);
}//GEN-LAST:event_jButtonRecognizeMouseClicked
    /**
     * Нажатие на кнопку "Добавить к обучению"
     * @param evt 
     */
private void jButtonAddToTrainingMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonAddToTrainingMouseClicked
    jComboBoxAddToTraining.removeAllItems();
    // Список уникальных имён нейронов
    HashSet<String> uniqueNames = new HashSet<String>();
    for (String name : facts.values()) {
        if (!uniqueNames.contains(name)) {
            jComboBoxAddToTraining.addItem(name);
        }
        uniqueNames.add(name);
    }

    jButtonAddToTraining.setVisible(false);

    jButtonTraining.setEnabled(false);
    jButtonRecognize.setEnabled(false);
    jComboBoxAddToTraining.setVisible(true);
    jButtonAddToTrainingOk.setVisible(true);
}//GEN-LAST:event_jButtonAddToTrainingMouseClicked
    /**
     * Нажатие на кнопку "ОК"
     * @param evt 
     */
private void jButtonAddToTrainingOkMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonAddToTrainingOkMouseClicked
        jComboBoxAddToTraining.validate();
    if (jComboBoxAddToTraining.getSelectedItem() != null) {

        boolean[] signals = paintGrid1.checkedSegments.clone();
        //
        // Похоже, что он тут ссылки проверяет, поэтому не пашет
        //
        // Если такой набор сигналов уже есть, то не надо добавлять
        //if (!facts.containsKey(signals)) {
        // Добавляем новый объект изучения
        facts.put(signals, jComboBoxAddToTraining.getSelectedItem().toString());
        System.out.println(jComboBoxAddToTraining.getSelectedItem().toString());
        //}
    }

    jButtonAddToTraining.setVisible(true);

    jButtonTraining.setEnabled(true);
    jButtonRecognize.setEnabled(true);

    jComboBoxAddToTraining.setVisible(false);
    jButtonAddToTrainingOk.setVisible(false);
}//GEN-LAST:event_jButtonAddToTrainingOkMouseClicked

private void paintGrid1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_paintGrid1MouseEntered
// TODO add your handling code here:
}//GEN-LAST:event_paintGrid1MouseEntered

private void mainPanelMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mainPanelMouseMoved
    paintGrid1.repaint();
}//GEN-LAST:event_mainPanelMouseMoved

    private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonClearActionPerformed
        paintGrid1.clear();
    }//GEN-LAST:event_jButtonClearActionPerformed

    private void jCheckBoxEqualZoomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxEqualZoomActionPerformed
        paintGrid1.isSqareSegments = jCheckBoxEqualZoom.isSelected();
    }//GEN-LAST:event_jCheckBoxEqualZoomActionPerformed

    private void jSliderPaintSizeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderPaintSizeStateChanged
        paintGrid1.PaintSize = jSliderPaintSize.getValue();
    }//GEN-LAST:event_jSliderPaintSizeStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAddToTraining;
    private javax.swing.JButton jButtonAddToTrainingOk;
    private javax.swing.JButton jButtonClear;
    private javax.swing.JButton jButtonRecognize;
    private javax.swing.JButton jButtonTraining;
    private javax.swing.JCheckBox jCheckBoxEqualZoom;
    private javax.swing.JComboBox jComboBoxAddToTraining;
    private javax.swing.JList jListRecognized;
    private javax.swing.JPanel jPanelAddToTraining;
    private javax.swing.JPanel jPanelButtons;
    private javax.swing.JPanel jPanelTools;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSlider jSliderPaintSize;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private recognizer.PaintGrid paintGrid1;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
}
